package com.qfedu.controller;

import com.qfedu.pojo.req.RolePermissionReq;
import com.qfedu.pojo.req.RoleReq;
import com.qfedu.pojo.resp.BaseResp;
import com.qfedu.service.RoleService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    RoleService roleService;

    @RequiresPermissions("SelectRole")
    @GetMapping("findAllRole")
    public BaseResp findAllRole(@RequestParam("page")Integer page, @RequestParam("size")Integer size) {
        return roleService.findAllRole(page, size);
    }

    @RequiresPermissions("UpdateRole")
    @PostMapping("addRole")
    public BaseResp addRole(@RequestBody RoleReq roleReq) {
        return roleService.addOrUpdRole(roleReq);
    }

    @RequiresPermissions("UpdateRole")
    @PostMapping("updRole")
    public BaseResp updRole(@RequestBody RoleReq roleReq) {
        return roleService.addOrUpdRole(roleReq);
    }

    @RequiresPermissions("UpdateRole")
    @PostMapping("delRole")
    public BaseResp delRole(@RequestBody RoleReq roleReq) {
        return roleService.delRole(roleReq);
    }

    //根据角色ID查询出所有权限以及用户所拥有的权限的方法
    @PostMapping("findAllPerAndRolePer")
    public BaseResp findAllPerAndRolePer(@RequestBody Map map) {
        return roleService.findAllPerAndRolePer(Integer.parseInt(map.get("id").toString()));
    }

    //对角色进行授权操作的方法
    @RequiresPermissions("UpdateToPermission")
    @PostMapping("updRolePermission")
    public BaseResp updRolePermission(@RequestBody RolePermissionReq rolePermissionReq) {
        return roleService.updRolePermission(rolePermissionReq);
    }
}
